package com.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.entity.Article;
import com.entity.Category;
import com.entity.Tag;
import com.entity.User;
import com.github.pagehelper.PageInfo;
import com.service.ArticleService;
import com.service.CategoryService;
import com.service.TagService;

import cn.hutool.core.lang.UUID;
import cn.hutool.http.HtmlUtil;

/**
 * 文章控制层
 * @author MingCha Yang
 *
 */
@Controller @RequestMapping("/article")
public class ArticleController {

	@Resource(name = "articleServiceImpl")
	private ArticleService articleService;
	@Resource(name = "categoryServiceImpl")
	private CategoryService categoryService;
	@Resource(name= "tagServiceImpl")
	private TagService tagService;

	@RequestMapping("/article-list")
	public String getarticle_list(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
			@RequestParam(required = false, defaultValue = "3") Integer pageSize, ModelMap m) {
		PageInfo<Article> pageInfo = articleService.pagelistArticle(pageNo, pageSize);

		m.put("pageInfo", pageInfo);
		m.put("pageUrlPrefix", "article/article-list?pageNo");
		return "Article/article-list";
	}

	@RequestMapping("/add")
	public String gotoaddpage(ModelMap m) {
		 List<Category> categorys= categoryService.getCategoryList();
		 List<Tag> tags= tagService.getTagList();
		 m.put("categorys", categorys);
		 m.put("tags", tags);
		return "Article/article-add";
	}

	@RequestMapping("/insert")
	public String gotoinsert(HttpServletRequest req) {
		
		Article article = new Article();

		article.setArticleCreateTime(new Date()); // 文章创建时间
		article.setArticleUpdateTime(new Date()); // 文章修改时间
		article.setArticleOrder(1);// 文章的排序
		article.setArticleIsComment(0);// 是否被评论
		article.setArticleLikeCount(0); // 点赞数量
		article.setArticleCommentCount(0);// 评论数量
		article.setArticleViewCount(0); // 查看数量
		User u = (User) req.getSession().getAttribute("user_session");
		article.setArticleUserId(u.getUserId());// 用户id
		article.setArticleTitle(req.getParameter("articleTitle"));// 标题
		article.setArticleContent(req.getParameter("articleContent"));// 内容
		String str = HtmlUtil.cleanHtmlTag(article.getArticleContent());
		article.setArticleSummary(str.length() > 150 ? str.substring(0, 150) : str);// 文章摘要
		article.setArticleStatus(Integer.parseInt(req.getParameter("articleStatus")));// 状态

		// 分类
		List<Category> categorys = new ArrayList<>(2);
		categorys.add(new Category(Integer.parseInt(req.getParameter("articleParentCategoryId"))));
		categorys.add(new Category(Integer.parseInt(req.getParameter("articleChildCategoryId"))));
		article.setCategorys(categorys);
		// 标签
		List<Tag> tags = new ArrayList<>();
		String[] tagIds = req.getParameterValues("articleTagIds");
		for (String tagId : tagIds) {
			tags.add(new Tag(Integer.parseInt(tagId)));
		}
        article.setTags(tags);
		articleService.addArticle(article);
		return "forward:/article/article-list";
	}
	/**
	 * 保存文章草稿
	 *
	 */
	@RequestMapping("/insertDraftSubmit")
	public String insertDraft(String articleTitle,String articleContent,HttpSession session) {
		Article article = new Article();
		article.setArticleCreateTime(new Date()); // 文章创建时间
		article.setArticleUpdateTime(new Date()); // 文章修改时间
		article.setArticleOrder(1);// 文章的排序
		article.setArticleIsComment(0);// 是否被评论
		article.setArticleLikeCount(0); // 点赞数量
		article.setArticleCommentCount(0);// 评论数量
		article.setArticleViewCount(0); // 查看数量
		User u = (User) session.getAttribute("user_session");
		article.setArticleUserId(u.getUserId());// 用户id
		article.setArticleTitle(articleTitle);// 标题
		article.setArticleContent(articleContent);// 内容
		String str = HtmlUtil.cleanHtmlTag(article.getArticleContent());
		article.setArticleSummary(str.length() > 150 ? str.substring(0, 150) : str);// 文章摘要
		article.setArticleStatus(0);// 状态
		article.setTags(new ArrayList<Tag>(0));
		article.setCategorys(new ArrayList<Category>(0));
		articleService.addArticle(article);
		return"forward:/article/article-list";
	}
	//重点，有两种实现方式
	@RequestMapping("/uploadimg") @ResponseBody
	public String uploadFile(MultipartHttpServletRequest req) throws IllegalStateException, IOException {
		//imgFile这个名称就是这么写的
		MultipartFile file = req.getFile("imgFile");
		//生成一个随机的文件名
		String newName = UUID.randomUUID().toString();
		File destFile =new File("H:/imguploads/"+newName);
		file.transferTo(destFile);
		String path="http://localhost:8080/uploads/"+newName;
		return "{\"error\":0,\"url\":\""+path+"\"}";
	}
	
}
